<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="学期" prop="semeId">
        <el-select v-model="queryParams.semeId" placeholder="请选择学期" :style="{width: '100%'}" @change="getList()">
          <el-option v-for="(item, index) in semeOptions" :key="index" :label="item.value"
                     :value="item.key" :disabled="item.disabled">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="分党校" prop="deptId">
        <el-select v-model="queryParams.deptId" placeholder="请选择分党校" filterable  @change="getList()"
                   :style="{width: '100%'}" >
          <el-option v-for="(item, index) in deptOptions" :key="index" :label="item.value"
                     :value="item.key" :disabled="item.disabled">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="真实姓名" prop="realName">
        <el-input
          v-model="queryParams.realName"
          placeholder="请输入真实姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学号" prop="hnusterId">
        <el-input
          v-model="queryParams.hnusterId"
          placeholder="请输入学号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核状态" prop="checked">
        <el-select v-model="queryParams.checked" placeholder="请选择审核状态" :style="{width: '100%'}" @change="getList()" @keyup.enter.native="handleQuery">
          <el-option :key="index" label="未审核"
                     :value="0">
          </el-option>
          <el-option :key="index" label="通过"
                     :value="1">
          </el-option>
          <el-option :key="index" label="未通过"
                     :value="2">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['psedu-base:apply:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['psedu-base:apply:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['psedu-base:apply:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['psedu-base:apply:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="applyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="申请ID" align="center" prop="applyId" />
      <el-table-column label="真实姓名" align="center" prop="realName"/>
      <el-table-column label="学号/工号" align="center" prop="hnusterId"/>
      <el-table-column label="党校用户ID" align="center" prop="psUserId" />
      <el-table-column label="本人关于党的信息" align="center" prop="partyData" />
      <el-table-column label="性别" align="center" prop="sex">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.sex"/>
        </template>
      </el-table-column>
      <el-table-column label="民族" align="center" prop="nation" />
      <el-table-column label="籍贯" align="center" prop="nativePlace" />
      <el-table-column label="学历" align="center" prop="eduBackground" />
      <el-table-column label="所在班级" align="center" prop="classes" />
      <el-table-column label="联系电话" align="center" prop="phone" />
      <el-table-column label="E-mail" align="center" prop="email" />
      <el-table-column label="QQ" align="center" prop="qq" />
      <el-table-column label="首次申请入党日期" align="center" prop="submitPaperDate" />
      <el-table-column label="审核状态" align="center" prop="checked"  fixed="right" width="90">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.apply_status" :value="scope.row.checked"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="120">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.checked !== 1"
            size="mini"
            type="text"
            icon="el-icon-circle-check"
            @click="handleApplyPass(scope.row)"
            v-hasPermi="['psedu-base:apply:edit']"
          >通过</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['psedu-base:apply:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['psedu-base:apply:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改培训报名对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="真实姓名" prop="realName" >
          <el-input v-model="form.realName" placeholder="请输入真实姓名" />
        </el-form-item>
        <el-form-item label="学号/工号" prop="hnusterId" >
          <el-input v-model="form.hnusterId" placeholder="请输入党校用户ID" />
        </el-form-item>
        <el-form-item label="党校用户ID" prop="psUserId" >
          <el-input v-model="form.psUserId" placeholder="请输入党校用户ID" />
        </el-form-item>
        <el-form-item label="报名学期ID" prop="semeId">
          <el-input v-model="form.semeId" placeholder="请输入报名学期ID" />
        </el-form-item>
        <el-form-item label="本人关于党的信息" prop="partyData">
          <el-input v-model="form.partyData" placeholder="请输入本人关于党的信息" />
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-select v-model="form.sex" placeholder="请选择性别">
            <el-option
              v-for="dict in dict.type.sys_user_sex"
              :key="dict.value"
              :label="dict.label"
:value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="民族" prop="nation">
          <el-input v-model="form.nation" placeholder="请输入民族" />
        </el-form-item>
        <el-form-item label="籍贯" prop="nativePlace">
          <el-input v-model="form.nativePlace" placeholder="请输入籍贯" />
        </el-form-item>
        <el-form-item label="学历" prop="eduBackground">
          <el-input v-model="form.eduBackground" placeholder="请输入学历" />
        </el-form-item>
        <el-form-item label="所在班级" prop="classes">
          <el-input v-model="form.classes" placeholder="请输入所在班级" />
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="E-mail" prop="email">
          <el-input v-model="form.email" placeholder="请输入E-mail" />
        </el-form-item>
        <el-form-item label="QQ" prop="qq">
          <el-input v-model="form.qq" placeholder="请输入QQ" />
        </el-form-item>
        <el-form-item label="首次申请入党日期" prop="submitPaperDate">
          <el-date-picker
            v-model="form.submitPaperDate"
            type="date"
            placeholder="选择首次申请入党日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="审核状态" prop="checked">
          <el-input v-model="form.checked" placeholder="请输入审核状态" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listApply, getApply, delApply, addApply, updateApply, applyPass } from "@/api/psedu-base/apply";
import { semeAndPsDeptOption } from "@/api/psedu-base/selectOption"

export default {
  name: "Apply",
  dicts: ['sys_user_sex', 'apply_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 培训报名表格数据
      applyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        realName: null,
        hnusterId: null,
        psUserId: null,
        semeId: null,
        nation: null,
        nativePlace: null,
        eduBackground: null,
        classes: null,
        checked: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      semeOptions: [],
      deptOptions: [],
    };
  },
  created() {
    this.getSemeAndPsDeptOption().then(res => {
      this.getList();
    });
  },
  methods: {
    getSemeAndPsDeptOption() {
      console.log('--------')
      return semeAndPsDeptOption().then(res => {
        this.deptOptions = res.data.deptList;
        this.semeOptions = res.data.semeList;
        this.queryParams.deptId = this.deptOptions[0].key
        this.queryParams.semeId = this.semeOptions[0].key
        return Promise.resolve();
      })
    },
    /** 查询培训报名列表 */
    getList() {
      this.loading = true;
      listApply(this.queryParams).then(response => {
        this.applyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        applyId: null,
        realName: null,
        psUserId: null,
        semeId: null,
        partyData: null,
        sex: null,
        nation: null,
        nativePlace: null,
        eduBackground: null,
        classes: null,
        phone: null,
        email: null,
        qq: null,
        submitPaperDate: null,
        checked: null,
        scores: null,
        summary: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.applyId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加培训报名";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const applyId = row.applyId || this.ids
      getApply(applyId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改培训报名";
      });
    },
    /** 通过申请 */
    handleApplyPass(row) {
      applyPass(row.applyId).then(res => {
        this.$modal.msgSuccess("修改成功");
        this.getList();
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.applyId != null) {
            updateApply(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addApply(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const applyIds = row.applyId || this.ids;
      this.$modal.confirm('是否确认删除培训报名编号为"' + applyIds + '"的数据项？').then(function() {
        return delApply(applyIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('psedu-base/apply/export', {
        ...this.queryParams
      }, `apply_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
